package com.smedi.ismedi.progress.core.port.adapter.service.impl;

import com.smedi.ismedi.constructionproject.api.dto.request.ProjectListQueryRequest;
import com.smedi.ismedi.constructionproject.api.dto.response.ProjectBasicResponse;
import com.smedi.ismedi.constructionproject.api.dto.response.resp.ProjectResp;
import com.smedi.ismedi.constructionproject.api.feign.RemoteConstructionProjectFeignApi;
import com.smedi.ismedi.progress.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.progress.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.progress.core.port.adapter.service.ProjectProgressService;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class ProjectProgressServiceImpl implements ProjectProgressService {
    private final RemoteConstructionProjectFeignApi remoteConstructionProjectFeignApi;

    private final AdapterConverter adapterConverter;

    @Override
    public  ResponseData<List<ProjectBasicResponse>> getProjectList(Long hrOrganizationId) {
        ResponseData<List<ProjectBasicResponse>> projectBasicResponse = remoteConstructionProjectFeignApi.getQueryProjectBasicListByHrOrganizationId(hrOrganizationId);
        if (!projectBasicResponse.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, projectBasicResponse.getApplication(), String.valueOf(projectBasicResponse.getErrorMsg()));
        }
        return projectBasicResponse;
    }
    @Override
    public  ResponseData<List<ProjectBasicResponse>> getQueryProjectBasicListByHrOrganizationIds(List<Long> hrOrganizationIds) {
        ResponseData<List<ProjectBasicResponse>> projectBasicResponse = remoteConstructionProjectFeignApi.getQueryProjectBasicListByHrOrganizationIds(hrOrganizationIds);
        if (!projectBasicResponse.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, projectBasicResponse.getApplication(), String.valueOf(projectBasicResponse.getErrorMsg()));
        }
        return projectBasicResponse;
    }

    @Override
    public ResponseData<PaginationData> getProjectList(Long offset, Long pageSize, String projectName, String projectCode, String projectManager, Long hrOrganizationId, String supervisionUnit, String projectStatus, String projectCategory, String contractDurationBegin, String contractDurationEnd) {
        return null;
    }

    @Override
    public ResponseData<ProjectBasicResponse> getQueryProjectBasicView(Long projectId) {
        ResponseData<ProjectBasicResponse> projectBasicResponse = remoteConstructionProjectFeignApi.getQueryProjectBasicView(projectId);
        if (!projectBasicResponse.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, projectBasicResponse.getApplication(), String.valueOf(projectBasicResponse.getErrorMsg()));
        }
        return projectBasicResponse;
    }

    @Override
    public List<ProjectResp> getProjectListWithoutToken(List<Long> ids) {
        ResponseData<List<ProjectResp>>  projectResp = remoteConstructionProjectFeignApi.getProjectListWithoutToken(ProjectListQueryRequest.builder().ids(ids).build());
        if (!projectResp.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, projectResp.getApplication(), String.valueOf(projectResp.getErrorMsg()));
        }
        return projectResp.getData();
    }

    @Override
    public List<ProjectBasicResponse> getAllProjectBasicList() {
        ResponseData<List<ProjectBasicResponse>> projectBasicResponse = remoteConstructionProjectFeignApi.getQueryAvailableProjectBasicList();
        if (!projectBasicResponse.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, projectBasicResponse.getApplication(), String.valueOf(projectBasicResponse.getErrorMsg()));
        }
        return projectBasicResponse.getData();
    }

    @Override
    public ResponseData<List<ProjectBasicResponse>> getProjectListByApp(String appSearch,List<Long> ids) {
        ResponseData<List<ProjectBasicResponse>> projectBasicResponse = remoteConstructionProjectFeignApi.getQueryProjectBasicListByApp(appSearch,ids);
        if (!projectBasicResponse.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, projectBasicResponse.getApplication(), String.valueOf(projectBasicResponse.getErrorMsg()));
        }
        return projectBasicResponse;
    }

    @Override
    public ProjectBasicResponse getProject(Long projectId) {
        ResponseData<ProjectBasicResponse> projectData =  remoteConstructionProjectFeignApi.getQueryProjectBasicView(projectId);
        if (!projectData.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, projectData.getApplication(), String.valueOf(projectData.getErrorMsg()));
        }
        return projectData.getData();
    }
}
